﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Keleyi.Com.IDAL;
using Keleyi.Com.EFDAL.Models;
using Keleyi.Com.Model;

namespace Keleyi.Com.EFDAL
{
    public class Message : IMessage
    {
        public string Add(string title, string content, string email, string qq)
        {
            using (var db = new cms_keleyi_comContext())
            {
                var k_message = new MessageInfo { kTitle = title, kContent = content, kEmail = email, kQQ = qq, kAddtime = DateTime.Now, kIsshow = false };

                if (k_message.kTitle.Trim() == string.Empty)
                    return "标题不能为空";
                if (k_message.kContent.Trim().Length < 10)
                    return "内容太少";
                if (k_message.kContent.Length > 8000)
                    return "内容太多";


                db.keleyi_message.Add(k_message);
                if (db.SaveChanges() == 1)
                    return string.Empty;
                else
                    return "出错";
            }
        }

        public IList<Keleyi.Com.Model.MessageInfo> GetList()
        {
            using (var db = new cms_keleyi_comContext())
            {
                var k_messages = from k_m in db.keleyi_message
                                 orderby k_m.kID descending
                                 where k_m.kIsshow == true
                                 select k_m;

                return k_messages.ToList();
            }
        }

        public IList<Keleyi.Com.Model.MessageInfo> GetListInside()
        {
            using (var db = new cms_keleyi_comContext())
            {
                var k_messages = from k_m in db.keleyi_message
                                 orderby k_m.kID descending
                                 select k_m;

                return k_messages.ToList();
            }
        }

        public bool TurnIsShow(int id, bool isshow)
        {
            using (var context = new cms_keleyi_comContext())
            {
                MessageInfo region = context.keleyi_message.First(v => v.kID == id);

                region.kIsshow = isshow;

                if (context.SaveChanges() == 1)
                    return true;
                else
                    return false;
            }
        }

    }
}